package main

import (
	"fmt"
)

func main() {

	arry := []int{1, 5, 4, 8, 7, 6, 3, 11}

	fmt.Println(arry[2])

	fmt.Println(findVal(arry, 3))
}

func findVal(arry []int, v int) int {
	for i, a := range arry {
		if a == v {
			return i
		}
	}
	return -1
}

func insertVal(arry []int, a int) bool {
	var res []int
	index := findVal(arry, a)
	if index > 0 {
		fmt.Println(arry[:index])
		res = append(res, arry[:index]...)
		res = append(res, a)
		res = append(res, arry[index:]...)
		fmt.Println(res)
		return true
	}
	return false
}

type Node struct {
	Data interface{}
	Next *Node
}

type List struct {
	lastNode *Node
	headNode *Node
	length   int
	count    int
}

func (l *List) IsEmpty() bool {
	return l.headNode == nil
}

func (l *List) Length() int {
	// cur := l.headNode
	// count := 0
	// for cur != nil {
	// 	count++
	// 	cur = cur.Next
	// }
	// return count
	return l.length
}

func (l *List) Add(data interface{}) *Node {
	node := &Node{Data: data}
	if l.IsEmpty() {
		l.headNode = node
		l.lastNode = node
	} else {
		node.Next = l.headNode
		l.headNode = node
	}
	l.length++
	return node
}

func (l *List) Append(data interface{}) {
	node := &Node{Data: data}
	if l.IsEmpty() {
		l.headNode = node
		l.lastNode = node
	} else {
		l.lastNode.Next = node
		l.lastNode = node
	}
	l.length++
}

func (l *List) findinList(v interface{}) bool {
	head := l.headNode
	for {
		if head.Data == v {
			return true
		}
		if head.Next == nil {
			return false
		}
		head = head.Next
	}
}

func (l *List) orderList() {
	cur := l.headNode
	for {
		cur = l.Change(cur)
		fmt.Println(cur.Data)
		if cur.Next == nil {
			break
		}
	}
}

func (l *List) Change(cur *Node) *Node {
	if cur.Next == nil {
		return nil
	}
	second := cur.Next
	l.count++
	if cur.Data.(int) > second.Data.(int) {
		tmp := cur.Data
		cur.Data = second.Data
		second.Data = tmp
		return l.headNode
	} else {
		return second
	}
}

type DulNode struct {
	Ele     interface{}
	preNode *DulNode
	nexNode *DulNode
}

type DulList struct {
	Node  *DulNode
	count int
}

func (l *DulList) Add(node *DulNode) {
	if l.Node == nil {
		l.Node = node
		return
	}
	cur := l.Node
	for {
		if cur.nexNode == nil {
			cur.nexNode = node
			node.preNode = cur
			break
		} else {
			cur = cur.nexNode
		}
	}
}

func (l *DulList) AddIndex(idx int, node *DulNode) {
	index := 0
	if l.Node != nil {
		cur := l.Node
		for {
			if index == idx {
				node.preNode = cur
				node.nexNode = cur.nexNode
				cur.nexNode.preNode = node
				cur.nexNode = node
				break
			} else {
				index++
				cur = cur.nexNode
			}
		}
	}
}

func (l *DulList) bubbleSort() {
	cur := l.Node
	if cur != nil {
		heigh := cur
		for {
			if l.run(heigh) {
				heigh = l.Node
			} else {
				heigh = heigh.nexNode
				if heigh == nil {
					break
				}
			}
		}
		fmt.Println(l.count)
	}
}

func (l *DulList) run(cur *DulNode) bool {
	haschange := false
	for {
		if cur == nil {
			break
		}
		l.count++
		p := cur.nexNode
		if p != nil {
			if p.Ele.(int) > cur.Ele.(int) {
				l.exChange(cur)
				haschange = true
			}
		}
		cur = cur.nexNode
	}
	return haschange
}

func (l *DulList) exChange(node *DulNode) {
	if node.nexNode == nil {
		return
	}
	tmp := node.Ele
	node.Ele = node.nexNode.Ele
	node.nexNode.Ele = tmp
}

func (l *DulList) Print() {
	cur := l.Node
	fmt.Println("打印：")
	for {
		fmt.Println(cur.Ele)
		if cur.nexNode == nil {
			break
		} else {
			cur = cur.nexNode
		}
	}
}
